
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       hal_tone_alarm.c
  * @author     baiyang
  * @date       2022-5-27
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "hal_tone_alarm.h"

#include <rtthread.h>
#include <rtdevice.h>

#include <common/gp_math/gp_mathlib.h>
/*-----------------------------------macro------------------------------------*/
#ifndef MB_ALARM_PWM_GROUP
#define MB_ALARM_PWM_GROUP " "
#endif

#ifndef MB_ALARM_PWM_CH
#define MB_ALARM_PWM_CH 1
#endif
/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/

/*----------------------------------variable----------------------------------*/
static uint8_t _toneAlarm_types;
#if defined(RT_USING_PWM)
static struct rt_device_pwm* _toneAlarm_pwm;
#endif
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
/**
  * @brief       
  * @param[in]   types  
  * @param[out]  
  * @retval      
  * @note        
  */
bool hal_tonealarm_init(uint8_t types)
{
    _toneAlarm_types = types;
#if !defined(RT_USING_PWM)
    // Nothing to do
    return false;
#else

    _toneAlarm_pwm = (struct rt_device_pwm *)rt_device_find(MB_ALARM_PWM_GROUP);

    if (_toneAlarm_pwm == NULL) {
        return false;
    }

    rt_device_open((rt_device_t)_toneAlarm_pwm, RT_DEVICE_OFLAG_RDWR);
    rt_pwm_disable(_toneAlarm_pwm, MB_ALARM_PWM_CH);

    return true;
#endif
}

/**
  * @brief       
  * @param[in]   frequency  
  * @param[in]   volume  
  * @param[in]   duration_ms  
  * @param[out]  
  * @retval      
  * @note        
  */
void hal_tonealarm_set_buzzer_tone(float frequency, float volume, uint32_t duration_ms)
{
#if defined(RT_USING_PWM)
    if (_toneAlarm_pwm != NULL) {
        if (math_flt_zero(frequency) || math_flt_zero(volume)) {
            rt_pwm_disable(_toneAlarm_pwm, MB_ALARM_PWM_CH);
        } else {
            // 周期、脉宽单位是纳秒
            rt_pwm_set(_toneAlarm_pwm, MB_ALARM_PWM_CH, roundf(1000000000/frequency), roundf(volume*1000000000/frequency)/2);
            rt_pwm_enable(_toneAlarm_pwm, MB_ALARM_PWM_CH);
        }
    }
#endif
}
/*------------------------------------test------------------------------------*/


